package com.action;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.config.PrimaryGenerater;
import com.dto.Apply;
import com.dto.BankImg;
import com.dto.Business;
import com.dto.CreditDto;
import com.dto.EmpInfo;
import com.dto.Imgtab;
import com.dto.UserInfo;
import com.service.ApplyService;
import com.service.ImgtabService;
import com.util.CreditUtile;
import com.util.FenYe;
import com.util.PageF;
import com.util.PageUtilLei;
import com.util.Query;

//贷款申请表
@RestController
@RequestMapping("ApplyAction")
public class ApplyAction {
	@Autowired
	private ApplyService applyService;
	@Autowired
	private PrimaryGenerater primaryGenerater;
	@Autowired
	private ImgtabService imgService;
	
	
	/**
	 * 填写您的贷款个人资料和上传相关证件照片
	 * @throws Exception 
	 */
	@RequestMapping("addApplyInfo.do")
	public ModelAndView addApplyInfo(String op,Apply apply,@RequestParam("pics") List<MultipartFile> pics,Imgtab imgtab,BankImg bankImg,@RequestParam("imgs") List<MultipartFile> imgs,HttpSession session,HttpServletRequest request) throws Exception {
		ModelAndView mv =new ModelAndView();
		String pay_num = primaryGenerater.toDateString();
		System.out.println("pay_num:"+pay_num);
		apply.setPay_num(pay_num);
		session.setAttribute("pay_num", pay_num);
		session.setAttribute("apply", apply);
		Object attribute = session.getAttribute("userInfo");//从session中获取到用户ID
		if(attribute != null) {
			UserInfo user = (UserInfo)attribute;
			apply.setU_id(user.getU_id());
		}
		
		//上传个人,企业,抵押贷款的相关图片
		if(pics.size()>0) {
			//上传银行流水
			imgtab.setPay_num(pay_num);
			for (int i = 0; i < pics.size(); i++) {
				MultipartFile pic = pics.get(i);
				//得到旧的文件名
				String oldFileName = pic.getOriginalFilename();
				
				//生成新的文件
				String newFileName=UUID.randomUUID().toString()+oldFileName.substring(oldFileName.lastIndexOf("."));
				//把新的文件名存入数据库
				if(i==0) {
					imgtab.setFrIDC(newFileName);
				}
				if(i==1) {
					imgtab.setReidc(newFileName);
				}
				if(i==2) {
					imgtab.setFrbank(newFileName);
				}
				if(i==3) {
					imgtab.setRebank(newFileName);
				}
				if(i==4) {
					imgtab.setDiyimg(newFileName);
				}
				//创建新的文件路径
				File newFile = new File("D:/static/img/"+newFileName);
				if(!newFile.exists()) {
					newFile.createNewFile();
				}
				try {
					pic.transferTo(newFile);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		
		
		if(imgs.size()>0) {
			bankImg.setPay_num(pay_num);
			for (int i = 0; i < imgs.size(); i++) {
				MultipartFile img = imgs.get(i);
				//得到旧的文件名
				String oldFileName1 = img.getOriginalFilename();
				//生成新的文件
				String newFileName1=UUID.randomUUID().toString()+oldFileName1.substring(oldFileName1.lastIndexOf("."));
				//把新的文件名存入数据库
				if(i==0) {
					bankImg.setName(newFileName1);
					System.out.println("--<"+newFileName1+">--");
				}
				if(i==1) {
					bankImg.setNameOne(newFileName1);
					System.out.println("--<"+newFileName1+">--");
				}
				//创建新的文件路径
				File newFile1 = new File("D:/static/img/"+newFileName1);
				if(!newFile1.exists()) {
					newFile1.createNewFile();
				}
				try {
					img.transferTo(newFile1);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}	
		int a = applyService.addApplyInfo(apply);
		int b=0;
		if(!"diya".equals(op)) {
			b = imgService.addBankImgpic(bankImg);
		}
		
		int c = imgService.addImgtab(imgtab);
		if(a!=0 && c!=0) {
			mv.setViewName("forward:/待审核页面.jsp");
		}else {
			mv.setViewName("/error");
		}
		return mv;
		
		}

		/**
	 * 填写公司贷款信息
	 * @throws Exception 
	 */
	@RequestMapping("addBusinessInfo.do")
	public ModelAndView addBusinessInfo(Business bus,@RequestParam("pics") List<MultipartFile> pics,Imgtab imgtab,BankImg bankImg,@RequestParam("imgs") List<MultipartFile> imgs,HttpSession session,HttpServletRequest request) throws Exception {
		ModelAndView mv =new ModelAndView();
		Object objUser = session.getAttribute("userinfo");
		String pay_num = primaryGenerater.toDateString();
		System.out.println("pay_num:"+pay_num);
		bus.setPay_num(pay_num);
		session.setAttribute("pay_num", pay_num);
		session.setAttribute("bus", bus);
		Object attribute = session.getAttribute("userInfo");//从session中获取到用户ID
		if(attribute != null) {
			UserInfo user = (UserInfo)attribute;
			bus.setU_id(user.getU_id());
		}
		
		//上传个人,企业,抵押贷款的相关图片
		if(pics.size()>0) {
			//上传银行流水
			imgtab.setPay_num(pay_num);
			for (int i = 0; i < pics.size(); i++) {
				MultipartFile pic = pics.get(i);
				//得到旧的文件名
				String oldFileName = pic.getOriginalFilename();
				
				//生成新的文件
				String newFileName=UUID.randomUUID().toString()+oldFileName.substring(oldFileName.lastIndexOf("."));
				//把新的文件名存入数据库
				if(i==0) {
					imgtab.setFrIDC(newFileName);
				}
				if(i==1) {
					imgtab.setReidc(newFileName);
				}
				if(i==2) {
					imgtab.setFrbank(newFileName);
				}
				if(i==3) {
					imgtab.setRebank(newFileName);
				}
				if(i==3) {
					imgtab.setBugimg(newFileName);
				}
				//创建新的文件路径
				File newFile = new File("D:/static/img/"+newFileName);
				if(!newFile.exists()) {
					newFile.createNewFile();
				}
				try {
					pic.transferTo(newFile);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		
		
		if(imgs.size()>0) {
			bankImg.setPay_num(pay_num);
			for (int i = 0; i < imgs.size(); i++) {
				MultipartFile img = imgs.get(i);
				//得到旧的文件名
				String oldFileName1 = img.getOriginalFilename();
				//生成新的文件
				String newFileName1=UUID.randomUUID().toString()+oldFileName1.substring(oldFileName1.lastIndexOf("."));
				//把新的文件名存入数据库
				if(i==0) {
					bankImg.setName(newFileName1);
					System.out.println("--<"+newFileName1+">--");
				}
				if(i==1) {
					bankImg.setNameOne(newFileName1);
					System.out.println("--<"+newFileName1+">--");
				}
				//创建新的文件路径
				File newFile1 = new File("D:/static/img/"+newFileName1);
				if(!newFile1.exists()) {
					newFile1.createNewFile();
				}
				try {
					img.transferTo(newFile1);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}	
		int a = applyService.addBusinessInfo(bus);
		int b = imgService.addBankImgpic(bankImg);
		int c = imgService.addImgtab(imgtab);
		if(a!=0 || b!=0 &&  c!=0) {
			mv.setViewName("forward:/待审核页面.jsp");
		}else {
			mv.setViewName("/error");
		}
		return mv;
		
		}

		//	 复审查询01   《ly》
	@RequestMapping("sss.do")
	public ModelAndView fushenApply(PageUtilLei pageLei){
		ModelAndView mv = new ModelAndView();
		Integer totalNum=applyService.queryAllApp(pageLei);//查总条数
		pageLei = new PageUtilLei(pageLei.getPageNum(),pageLei.getPageSize(),totalNum,pageLei);
		List<Apply> appList = applyService.fuShen(pageLei);//调用复审方法
		mv.addObject("pageUtil", pageLei);
		mv.addObject("appList", appList);//将结果集放入request中
		mv.setViewName("forward:/fs.jsp");
		return  mv;
	}
	
	//复审调查征信的方法
	@RequestMapping("zxjk.do")
	public List<CreditDto> zxjk(String realname,String idcard) throws Exception {
		List<CreditDto> zxList = CreditUtile.Get(realname, idcard);
		return zxList;
	}
	
	
	
	/**
	 * @时间2018年4月20日
	 * @作者:雷杨
	 * 说明:信用贷  通过复审
	 */
	@RequestMapping("OKApplyFs.do")
	public ModelAndView OKApplyFs(String pay_num) {
		ModelAndView mv = new ModelAndView();
		int x = applyService.OKApplyFs(pay_num);
		if(x > 0) {
			mv.setViewName("redirect:/ApplyAction/sss.do");
		}
		return mv;
	}
	
	/**
	 * @时间2018年4月20日
	 * @作者:雷杨
	 * 说明:信用贷  未通过复审 并返回驳回理由
	 */
	@RequestMapping("noApplyFs.do")
	public  ModelAndView noApplyFs(String pay_num,HttpSession sess) {
		ModelAndView mv = new ModelAndView();
		Map<String,Object> sur = new HashMap<String,Object>();
		UserInfo userInfo = (UserInfo) sess.getAttribute("userInfo");//获取用户session中的值
		EmpInfo empInfo = (EmpInfo) sess.getAttribute("emp");//获取员工session中的值
		sur.put("e_id", empInfo.getE_id());
		sur.put("u_id", userInfo.getU_id());
		int x = applyService.noApplyFs(pay_num);
		if(x > 0) {
			mv.setViewName("redirect:/ApplyAction/sss.do");
		}
		return mv;
	}
	
	
	
	/**
	 * @作者: 杨成龙
	 * @时间: 2018年4月16日
	 * @说明: 查询用户申请详情信息.信用
	 */
	@RequestMapping("applyList.do")
	public ModelAndView queryHouseList(HttpSession sess,PageF pageF,String pageNum){
		ModelAndView ma = new ModelAndView();
    	Integer totalNum = applyService.queryTotalNum();
    	List<Apply> applyList = new ArrayList<Apply>();
    	List<Imgtab> imgtabList = new ArrayList<Imgtab>();
		List<BankImg> bantabList = new ArrayList<BankImg>();
    	pageF = new PageF(pageF.getPageNum(),pageF.getPageSize(),totalNum);
    	applyList = applyService.queryAllApply(pageF);
    	imgtabList = applyService.queryFile("Imgtab");
		bantabList = applyService.queryLsFile("BankImg");
    	

    	
    	ma.addObject("bantabList", bantabList);
        ma.addObject("imgtabList", imgtabList);
    	ma.addObject("applyList", applyList);
        ma.addObject("pageF", pageF);
        ma.setViewName("forward:/chuShen01.jsp");
		return ma;     
	} 
	
	/**
	 * @作者: 杨成龙
	 * @时间: 2018年4月16日
	 * @说明: 查询用户申请详情信息.抵押
	 */
	@RequestMapping("applyList2.do")
	public ModelAndView queryHouseList2(HttpSession sess,PageF pageF,String pageNum){
		ModelAndView ma = new ModelAndView();
    	Integer totalNum = applyService.queryTotalNum();
    	List<Apply> applyList = new ArrayList<Apply>();
    	List<Imgtab> imgtabList = new ArrayList<Imgtab>();
    	pageF = new PageF(pageF.getPageNum(),pageF.getPageSize(),totalNum);
    	applyList = applyService.queryAllApplydy(pageF);
    	imgtabList = applyService.queryFile("Imgtab");
        ma.addObject("imgtabList", imgtabList);
    	ma.addObject("applyList", applyList);
        ma.addObject("pageF", pageF);
        ma.setViewName("forward:/chuShen02.jsp");
		return ma;     
	} 
	
	/**
	 * @作者: 杨成龙
	 * @时间: 2018年4月16日
	 * @说明: 查询用户申请详情信息.企贷
	 */
	@RequestMapping("/applyList3.do")
	public ModelAndView queryHouseList3(HttpSession sess,PageF pageF,String pageNum){
		ModelAndView ma = new ModelAndView();
    	Integer totalNum = applyService.queryTotalNumqy();
    	List<Apply> applyList = new ArrayList<Apply>();
    	List<Imgtab> imgtabList = new ArrayList<Imgtab>();
    	List<BankImg> bantabList = new ArrayList<BankImg>();
    	pageF = new PageF(pageF.getPageNum(),pageF.getPageSize(),totalNum);
    	applyList = applyService.queryAllApplyqy(pageF);
    	imgtabList = applyService.queryFile("Imgtab");
    	bantabList = applyService.queryLsFile("BankImg");
    	ma.addObject("bantabList", bantabList);
        ma.addObject("imgtabList", imgtabList);
    	ma.addObject("applyList", applyList);
        ma.addObject("pageF", pageF);
        ma.setViewName("forward:/chuShen03.jsp");
		return ma;     
	} 
	
	
	/**
	 * @作者: 杨成龙
	 * @时间: 2018年4月16日
	 * @说明: 审核通过
	 */
	@RequestMapping("/applyApprove.do")
	public ModelAndView approve(String pay_num){
		ModelAndView ma = new ModelAndView();
    	int a = applyService.approve(pay_num);  
    	if(a == 1) {
    		return new ModelAndView("redirect:/ApplyAction/applyList.do");
    		//ma.setViewName("/chuShen01");
    	}else {
    		ma.setViewName("/Error");
    	}
		return ma;     
	}
	
	/**
	 * @作者: 杨成龙
	 * @时间: 2018年4月16日
	 * @说明: 审核未通过
	 */
	@RequestMapping("/applyNoApprove.do")
	public ModelAndView Noapprove(HttpSession sess,String pay_num){
		ModelAndView ma = new ModelAndView();
		Map<String,Object> sur = new HashMap<String,Object>();
		UserInfo userInfo = (UserInfo) sess.getAttribute("userInfo");
		EmpInfo empInfo = (EmpInfo) sess.getAttribute("emp");
		/*sur.put("e_id", 1);
		sur.put("u_id", 2);*/
		sur.put("e_id", empInfo.getE_id());
		sur.put("u_id", userInfo.getU_id());

		int b = applyService.addSurvey(sur);
    	int a = applyService.Noapprove(pay_num);  
    	if(a == 1) {
    		return new ModelAndView("redirect:/ApplyAction/applyList.do");
    		//ma.setViewName("/chuShen01");
    	}else {
    		ma.setViewName("/Error");
    	}
		return ma;     
	}
	
	/**
	 * @时间2018年4月20日
	 * @作者:唐文亭
	 * 说明:查询所有贷款信息带分页
	 */
	@RequestMapping("queryApplyByFy.do")
	public ModelAndView queryApplyByFy(Apply apply,FenYe fy,Query query,HttpSession session) {
		ModelAndView mav=new ModelAndView();
		fy.setQuery(query);
		List<Apply> applys=applyService.queryApplyByFy(fy);
		if(applys!=null) {
			session.setAttribute("applys", applys);
			mav.addObject("fy", fy);
			mav.addObject("query", query);
			mav.setViewName("forward:/StoreManage.jsp");
		}else {
			mav.setViewName("Error");
		}
		return mav;
	}
	
	
}
